set -xe

# --------------------------
# 1. Build Glad (Shared + Static)
# --------------------------
# Glad Static Library (libglad.a)
gcc -c -fPIC vendor/glad/src/glad.c -I vendor/glad/include/ -o lib/glad.o
ar rcs lib/libglad.a lib/glad.o  # Create static lib from object file

# Glad Shared Library (libglad.so) (preserve original)
gcc -fPIC -shared vendor/glad/src/glad.c -I vendor/glad/include/ -o lib/libglad.so


# --------------------------
# 2. Build Runara (Shared + Static)
# --------------------------
# Get Freetype/Harfbuzz flags via pkg-config (reused for both lib types)
PKG_FLAGS=$(pkg-config --cflags --libs freetype2 harfbuzz)

# Runara Static Library (librunara.a)
gcc -c -fPIC runara.c $PKG_FLAGS -I vendor/glad/include/ -I vendor/linesky/ -I include/runara/ -march=native -ffast-math -o lib/runara.o
ar rcs lib/librunara.a lib/runara.o  # Create static lib from object file

# Runara Shared Library (librunara.so) (preserve original)
gcc -fPIC -shared runara.c $PKG_FLAGS -I vendor/glad/include/ -I vendor/linesky/ -I include/runara/ -march=native -ffast-math -o lib/librunara.so


# --------------------------
# 3. Build simple.c (Dynamic Link + Static Link)
# --------------------------
# 3.1 Dynamic Link (original behavior: links .so libs)
gcc simple.c -o simple_dynamic \
  -I vendor/glad/include/ -I include/runara/ \
  -I/usr/include/harfbuzz -I/usr/include/freetype2 -I/usr/include/libpng16 \
  -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include \
  -L lib/ -lrunara -lglad \
  -lglfw -lharfbuzz -lfreetype -lm

# 3.2 Static Link (links .a libs; uses -Wl,-Bstatic to force static linking)
gcc simple.c -o simple_static \
  -I vendor/glad/include/ -I include/runara/ \
  -I/usr/include/harfbuzz -I/usr/include/freetype2 -I/usr/include/libpng16 \
  -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include \
  -L lib/ \
  -Wl,-Bstatic -lrunara -lglad -Wl,-Bdynamic \
  -lglfw -lharfbuzz -lfreetype -lm

gcc simple.c -o simple_static \
  -I vendor/glad/include/ -I include/runara/ \
  -I/usr/include/harfbuzz -I/usr/include/freetype2 -I/usr/include/libpng16 \
  -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include \
  -L lib/ \
  -l:librunara.a -l:libglad.a \
  -lglfw -lharfbuzz -lfreetype -lm

# Cleanup intermediate object files (optional)
rm -f lib/glad.o lib/runara.o